package com.atguigu.guli.service.trade.controller.api;


import com.atguigu.guli.service.base.helper.JwtHelper;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author Miluo
 * @since 2020-11-06
 */
@RestController
@Slf4j
@Api(tags = "订单管理")
@RequestMapping("/api/trade/order")
public class ApiOrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "删除订单")
    @DeleteMapping("auth/remove/{orderId}")
    public R removeOrderByOrderId(@PathVariable String orderId,HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        Boolean isRemove = orderService.removeOrderById(orderId,memberId);
        if (isRemove){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }


    @ApiOperation(value = "获取当前用户订单列表")
    @GetMapping("/auth/list")
    public R getOrderByMemberId(HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        List<Order> orderList = orderService.getOrderByMemberId(memberId);
        return R.ok().data("items",orderList);
    }



    @ApiOperation( "判断课程是否购买")
    @GetMapping("auth/is-buy/{courseId}")
    public R isBuyByCourseId(@PathVariable String courseId,HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        Boolean isBuy = orderService.isBuyByCourseId(courseId,memberId);
        return R.ok().data("isBuy",isBuy);
    }



    @ApiOperation("根据ID获取课程信息")
    @GetMapping("/auth/get/{orderId}")
    public R getCourseById(@PathVariable String orderId,HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        Order order = orderService.getByOrderId(orderId,memberId);
        return R.ok().data("item",order);
    }


    @ApiOperation("新增订单")
    @PostMapping("/auth/save/{courseId}")
    public R save(@PathVariable String courseId, HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        String orderId = orderService.saveOrder(courseId,memberId);
        return R.ok().data("orderId",orderId);
    }

}

